<% layout("/inc/layout_page.html"){ %>

<form action="${BASE}/admin/cms/CmsColumns/updateSort" id="form_updateSort" method="post"> 
<div class="box">
  <div class="box-header">
    <h4 class="box-title"><i class="fa fa-bars"></i> 栏目列表</h4>
    <div class="box-tools">
      <span onclick="list_add(0)" class="btn btn-info btn-sm"><i class="fa fa-plus-square"></i> 添加栏目</span>
      <button type="submit" class="btn btn-success btn-sm"><i class="fa fa-sort-numeric-desc"></i> 保存排序</button>
    </div>
  </div>
  <div class="box-body no-padding">
    <table id="table_list" class="table table-border table-bordered table-bg table-striped table-hover">
      <thead>
        <tr class="text-c">
          <th>名称</th>
          <th width="60">排序</th>
          <th>链接</th>
          <th width="80">类型</th>
          <th width="228px">操作</th>
        </tr>
      </thead>
      <tbody id="tbody_table_rows"></tbody>
    </table>
  </div>
</div>
</form>

<script type="text/javascript">
var parid = 0;
function renderTr(jsonData,parentid){
  	for(var i in jsonData){
  		var html = "";
		var tr = $("<tr></tr>").addClass("treegrid-"+jsonData[i].id);
		if(parentid>0){
			tr.addClass("treegrid-parent-"+parentid);
		}
		html = html + '<td nowrap>'+jsonData[i].name+'</td>';
		html = html + '<td>'
		            + '<input type="hidden" name="ids" value="'+jsonData[i].id+'"/>'
		            + '<input name="sorts" type="text" value="'+jsonData[i].sort+'" style="width:60px">'
		            + '</td>';
		html = html + '<td>';
		if(jsonData[i].href)
			html += jsonData[i].href;
		html += '</td>';
		html = html + '<td class="text-c">';
		if("0"==jsonData[i].showModes){
			html = html + '栏目';
		}else if("0"==jsonData[i].showModes){
			html = html + '单页';
		}else{
			html = html + '链接';
		}
		html = html +'</td>';
		html = html + '<td class="text-c" width="200">';
		html = html +'<a class="btn btn-success btn-xs" href="javascript:;" title="添加子栏目" onclick="list_add('+jsonData[i].id+')"><i class="fa fa-plus-square-o"></i> 添加子栏目</a> ';
		html = html +'<a class="btn btn-primary btn-xs" href="javascript:;" title="编辑" onclick="list_edit('+jsonData[i].id+')"><i class="fa fa-pencil-square-o"></i> 编辑</a> ';
		html = html +'<a class="btn btn-danger btn-xs" href="javascript:;" title="删除" onclick="list_del('+jsonData[i].id+','+jsonData[i].isParent+')"><i class="fa fa-trash-o"></i> 删除</a>';
		html = html +'</td>';
			
		tr.html(html);
		tr.appendTo($('#tbody_table_rows'));
		if(jsonData[i].isParent){
			renderTr(jsonData[i].children,jsonData[i].id);
		}
  	}
}

function loadTree(){
	$.getJSON("${BASE}/admin/cms/CmsColumns/treeData",{parentId:parid},function(jsondata){
		$('#tbody_table_rows').text("");
		renderTr(jsondata,0);
		$('#table_list').treegrid();
	});
}

function list_add(pid){
	var index = layer.open({
		type: 2,
		closeBtn: 0,
		title: false,
		area: ['98%', '98%'],
		content: "${BASE}/admin/cms/CmsColumns/add?pid="+pid
	});
	layer.full(index);
}

function list_del(id,isp){
	if(isp){
		toastr.error('该栏目有子栏目，无法删除！');
		return false;
	}
	layer.confirm('确认删除所选记录?', {icon: 3, title:'警告'}, function(index){  
	    layer.close(index);
	    layer.load(1, {shade: [0.3,'#fff']});
	    $.post("${BASE}/admin/cms/CmsColumns/delete",{ids:id},function(jsondata){
			layer.closeAll('loading');
			if( jsondata.code == "001" ){
				toastr.success('删除成功！');
				loadTree();
			}else{
				toastr.error(jsondata.msg);
			}
	    },"json");
	});
}
function list_edit(id){
	var index = layer.open({
		type: 2,
		closeBtn: 0,
		title: false,
		area: ['98%', '98%'],
		content: "${BASE}/admin/cms/CmsColumns/edit?id="+id
	});
	layer.full(index);
}
function list_show(id){
    var index = layer.open({
		type: 2,
		closeBtn: 1,
		title: "栏目表",
		area: ['98%', '98%'],
		content: "${BASE}/admin/cms/CmsColumns/show?id="+id
	});
}
function form_search_reset() {
	$("#form_search")[0].reset();
	reload_table();
}

$(function(){
	loadTree();
	$("#form_updateSort").validate({
		errorPlacement: function(error, element) {
			error.appendTo(element.parent());
		},
		submitHandler : function(form) {
			layer.load(1, {shade: [0.3,'#fff']});
			$(form).ajaxSubmit({
				dataType:"json",
				success:function( jsondata ){
					layer.closeAll('loading');
					if(jsondata.success){
						toastr.success('成功！');
					}else{
						toastr.error('出错了！');
					}
				}
			});
		},
		rules : {
			sorts : {
				digits : true,
				min:0
			}
		}, 
		messages : { 
			sorts : { 
				digits : "只能为整数值",
				min : "最小值为0"
			}
		}
	});
})

</script>
<% } %>